<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>志愿者信息管理页面</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
    <link href="/static/css/default.css" rel="stylesheet" type="text/css" />
    <style>
        .discell {
            text-align:right;
        }
    </style>
</head>
<body>
<div class="mini-fit" style="margin-bottom: 40px;">
    <form id="form1" method="post" style="height:100%;width: 100%;overflow-x: auto;overflow-y: auto;">
        <!-- 编辑框START-->
        <div id="editform" class="form" style="padding:10px;">
            <input name="id" class="mini-hidden" style="width: 100%;" />
            <input name="createUser" class="mini-hidden" style="width: 100%;" value="${createUser!''}" />
            <input name="adduserid" class="mini-hidden" style="width: 100%;" value="${adduserid!''}" />
            <input name="addusername" class="mini-hidden" style="width: 100%;" value="${addusername!''}" />
            <input name="departid" class="mini-hidden" style="width: 100%;" value="${departid!''}" />
            <table class="form-table" align="center" cellspacing="8">
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>姓名:</td>
                    <td>
                        <input name="xm" id="xm" class="mini-textbox" style="width: 200px" required="true" />
                    </td>
                    <td class="discell"><font color="#dc143c">*</font>性别:</td>
                    <td class="input">
                        <input name="xb" id="xb" class="mini-combobox" required="true"
                               textField="codeName" valueField="codeId" emptyText="请选择..." style="width: 80%;"
                               url="/azbj/inmates/baseinfo/getTbsysCode?codeType=2_2&pcodeId=-1&sn=1"
                        />
                    </td>
                </tr>
                <tr>
                    <td class="discell">身份证号:</td>
                    <td>
                        <input name="sfzh" id="sfzh" class="mini-textbox" style="width: 200px" onvaluechanged="onMouseOut()" />
                    </td>
                    <td class="discell">年龄:</td>
                    <td>
                        <input name="nl" id="nl" class="mini-textbox" style="width: 200px" emptyText="输入身份证自动计算,禁止手工输入" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">志愿者手机号码:</td>
                    <td>
                        <input id="phone" name="lxfs" class="mini-textbox" style="width: 200px" onvaluechanged="checkPhone(1)"  emptyText="请输入手机号码"/>
                    </td>
                    <td class="discell">志愿者固话:</td>
                    <td>
                        <input id="tel" name="tel" class="mini-textbox" style="width: 200px" onvaluechanged="checkPhone(2)" emptyText="请输入固定电话"/>
                    </td>
                </tr>
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>职业:</td>
                    <td>
                        <input name="zhiye" class="mini-textbox" style="width: 200px" required="true" />
                    </td>
                    <td class="discell">工作单位及职务:</td>
                    <td>
                        <input name="gzdwjzw" class="mini-textbox" style="width: 200px" />
                    </td>
                </tr>
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>学历:</td>
                    <td>
                        <input name="xl" id="xl" class="mini-combobox" style="width: 200px" required="true"
                               textField="codeName" valueField="codeId" emptyText="请选择..."
                               url="/azbj/shzzgl/socialOrgInfo/getComboboxData?codeType=2_5"
                        />
                    </td>
                    <td class="discell"><font color="#dc143c">*</font>帮教技能:</td>
                    <td>
                        <input id="bjjn" name="bjjn" class="mini-textbox" style="width: 200px" required="true" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">住址:</td>
                    <td colspan="3">
                        <input id="zz" name="zz" class="mini-textbox" style="width: 100%" />
                    </td>
                </tr>
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>
                        所属单位:
                    </td>
                    <td colspan="3" class="input">
                        <input id="province" name="province" class="mini-combobox" required="true" emptyText="省份" style="width:117px;"
                               textfield="codeName" valueField="codeId"
                               onclick="onDeptChanged('province')" onvaluechanged="onValueChanged('province')" />
                        <input id="city" name="city" class="mini-combobox" required="true" emptyText="市" style="width: 117px"
                               textfield="codeName" valueField="codeId" onclick="onDeptChanged('city')"
                               onvaluechanged="onValueChanged('city')" />
                        <input id="county" name="county" class="mini-combobox" required="true" emptyText="县" style="width: 117px"
                               textfield="codeName" valueField="codeId" onclick="onDeptChanged('county')"
                               onvaluechanged="onValueChanged('county')" />
                        <input id="clab" name="clab" class="mini-combobox" required="true" emptyText="街道" style="width: 117px"
                               textfield="codeName" valueField="codeId"onclick="onDeptChanged('clab')" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">是否参与过帮扶工作:</td>
                    <td class="input">
                        <input name="sfcy" id="sfcy" class="mini-radiobuttonlist"
                               data="[{ id: 1, name: '是' },{ id: 2, name: '否' }]"
                               textField="name" valueField="id" value="1" />
                    </td>
                    <td class="discell">是否五老:</td>
                    <td class="input">
                        <input name="sfwl" id="sfwl" class="mini-radiobuttonlist"
                               data="[{ id: 1, name: '是' },{ id: 2, name: '否' }]"
                               textField="name" valueField="id" value="1" />
                    </td>
                </tr>
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>帮扶范围:</td>
                    <td>
                        <input name="bffw" id="bffw" class="mini-combobox" style="width: 200px"
                               textField="codeName" valueField="codeId" emptyText="请选择..." onvaluechanged="bffwInputClick()"
                               url="/azbj/bffw/getBffw?flog=${flog}" required="true" />
                        <input id="bffwCode" name="bffwCode" class="mini-combobox" required="true" emptyText="下级地区编码" style="width: 24%;"
                               textfield="codeName" valueField="codeId" onvaluechanged="checkBffwCode" multiSelect="true"/>
                    </td>
                </tr>
            </table>
        </div>
        <!--编辑框END-->
    </form>
</div>

<div style="width:100%; position: absolute;bottom: 0;line-height: 40px;border-top: 1px solid #dfdfdf;text-align: center;">
    <#if flog!='1'>
        <a class="mini-button" iconCls="fa-save" plain="true" onclick="saveData()">保存</a>
    </#if>
    <a class="mini-button" iconCls="fa-undo" plain="true" onclick="onCancel()">取消</a>
</div>

<script type="text/javascript">
    mini.parse();
    var form = new mini.Form("form1");
    var createUser = mini.getByName("createUser").getValue();
    var adduserid = mini.getByName("adduserid").getValue();
    var addusername = mini.getByName("addusername").getValue();
    var province = mini.get("province");
    var city = mini.get("city");
    var county = mini.get("county");
    var clab = mini.get("clab");
    var departid = mini.getByName("departid").getValue();
    var bffw = mini.get('bffw');
    var bffwCode = mini.get('bffwCode');
    var bffwCodeData = [{codeId: '-1', codeName: '全区域'}];

    $(document).ready(function () {
        if(bffw.getData()[0].codeId==4){
            $('#bffwCode').hide();
        }
        mini.get("nl").setEnabled(false);
        addressLocking();
    });

    //地址锁定
    function addressLocking(){
        $.ajax({
            url: "/azbj/getAddressLocking",
            type: "GET",
            success: function (res) {
                if(res.level == "06" || res.level == "07" || res.level == "08" || res.level == "03"){
                    province.setData([{"codeId": res.shengCode, "codeName": res.shengName}]);
                    province.setValue(res.shengCode);
                    province.setEnabled(false);
                }
                if(res.level == "07" || res.level == "08" || res.level == "03"){
                    city.setData([{"codeId": res.dishiCode, "codeName": res.dishiName}]);
                    city.setValue(res.dishiCode);
                    city.setEnabled(false);
                }
                if(res.level == "08" || res.level == "03"){
                    county.setData([{"codeId": res.quxianCode, "codeName": res.quxianName}]);
                    county.setValue(res.quxianCode);
                    county.setEnabled(false);
                }
                if(res.level == "03"){
                    clab.setData([{"codeId": res.jiedaoCode, "codeName": res.jiedaoName}]);
                    clab.setValue(res.jiedaoCode);
                    clab.setEnabled(false);
                }
            }
        });
    }

    //标准方法接口定义
    function SetData(data) {
        if (data.xm) {
            mini.get("xm").setEnabled(false);
        }
        var o = mini.decode(data);
        var lxfs = o.lxfs;
        form.setData(o);
        var id = data.id;
        $.ajax({
            url: "/azbj/zyzxxgl/volunteerInfoManage/selectById?id=" + id,
            type: "get",
            success: function (success) {
                mini.get("sfzh").setValue(success.data.sfzh);
            }
        });
        form.setChanged(false);
        if (${flog!''} == 1) {
            form.setEnabled(false);
        }
        if(mini.get('xm').getValue()!='' && mini.get('xm').getValue()!=undefined ){
            setBffwCode(bffwCode.getValue());
        }
        //设置省市县回显
        province.setData([{"codeId": data.province, "codeName": data.provinceName}]);
        city.setData([{"codeId": data.city, "codeName": data.cityName}]);
        county.setData([{"codeId": data.county, "codeName": data.countyName}]);
        clab.setData([{"codeId": data.clab, "codeName": data.clabName}]);
    }

    //帮扶范围点击
    function bffwInputClick(){
        if(province.getValue()=='' || city.getValue()=='' || county.getValue()=='' || clab.getValue()==''){
            mini.alert('请填写工作地址及详细地址信息');
            mini.get('bffw').setValue(null);
        }else {
            setBffwCode(bffwCode.getValue());
        }
    }

    function setBffwCode(bffwCodeId){
        bffwCodeData = [{codeId: '-1', codeName: '全区域'}];
        if(bffw.getValue()==1){
            bffwCode.setText('下级地市');
            if(city.getData().length==0){
                onDeptChanged('city')
            }
            bffwCodeData = bffwCodeData.concat(city.getData())
            $('#bffwCode').show();
        }else if(bffw.getValue()==2){
            bffwCode.setText('下级区县');
            if(county.getData().length==0){
                onDeptChanged('county')
            }
            bffwCodeData = bffwCodeData.concat(county.getData())
            $('#bffwCode').show();
        }else if(bffw.getValue()==3) {
            bffwCode.setText('下级街道');
            if(clab.getData().length==0){
                onDeptChanged('clab')
            }
            bffwCodeData = bffwCodeData.concat(clab.getData())
            $('#bffwCode').show();
        }else {
            $('#bffwCode').hide();
        }
        bffwCode.setData(bffwCodeData);
        if(bffwCodeId=='' || bffwCodeId==undefined || bffwCodeId=='无'){
            bffwCode.setValue('-1');
        }
    }

    //检查是否包含-1
    function checkBffwCode(){
        if(bffwCode.getValue().indexOf('-1')!==-1){
            bffwCode.setValue('-1');
        }
    }

    function getTelAndPhone(o) {
        var telAndPhone = o.lxfs;
        var tel, phone;
        if (telAndPhone !== '' && (/^1[3456789]\d{9}$/.test(telAndPhone))) {
            phone = telAndPhone;
        }
        if (telAndPhone !== '' && /^((0\d{2,3})-?)(\d{7,8})$/.test(telAndPhone)) {
            tel = telAndPhone;
        }
        if (telAndPhone.indexOf("|") !== -1) {
            var a = telAndPhone.split("|");
            tel = a[0];
            phone = a[1];
        }
        return {'tel': tel, 'phone': phone};
    }

    function saveData() {
        //验证非空字段
        form.validate();
        if (form.isValid() == false || ${flog!''} == 1) {
            return;
        }
        var data = form.getData();
        //验证身份证号
        if (data.sfzh) {
            var IDCardsValResult = onIDCardsValidation(data.sfzh);
            if (IDCardsValResult == false) {
                mini.alert("身份证号码格式不正确!")
                return;
            }
        }
        var tel = data.tel;
        var phone = data.lxfs;
        var newPhone = checkTelAndPhone(tel, phone);
        var flag = newPhone['flag'];
        var kflag = newPhone['kflag'];
        if ('-1' === kflag) {
            mini.alert("请至少输入一个联系方式");
            return;
        }
        if (flag) {
            data.lxfs = newPhone['newTelAndPhone'];
        } else {
            mini.alert("联系电话或手机号有误，请重填");
            return;
        }
        data.createUser = createUser;
        data.adduserid = adduserid;
        data.addusername = addusername;
        data.departid = departid;
        var json = mini.encode(data);
        commonSaveData("/azbj/zyzxxgl/volunteerInfoManage/save", {data: json}, callback);
        // 保存成功的回调函数
        function callback() {
            mini.alert("保存成功", "成功", window.CloseOwnerWindow);
        }
    }

    var urltemp = "/azbj/getCodeDetail/?codeType=2_1&codeId=-2";
    function onDeptChanged(e) {
        if ("province" == e) {
            var value = province.getValue();
            province.setValue("");
            //flag=1，代表只查询本系统的省份
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=-1&flag=1";
            province.setUrl(url);
            if (value != '') {
                province.setValue(value);
            } else {
                province.select(0);
            }
        }
        if ("city" == e) {
            var value = city.getValue();
            if (province.getValue() == "") {
                province.setUrl(urltemp);
                province.setValue("");
                county.setUrl(urltemp);
                county.setValue("");
                clab.setUrl(urltemp);
                clab.setValue("");
                return;
            }
            var codeId = province.getValue();
            city.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            city.setUrl(url);
            if (value != '') {
                city.setValue(value);
            } else {
                city.select(0);
            }
        }

        if ("county" == e) {
            var value = county.getValue();
            if (city.getValue() == "") {
                county.setUrl(urltemp);
                county.setValue("");
                clab.setUrl(urltemp);
                clab.setValue("");
                return;
            }
            var codeId = city.getValue();
            county.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            county.setUrl(url);
            if (value != '') {
                county.setValue(value);
            } else {
                county.select(0);
            }
        }

        if ("clab" == e) {
            var value = clab.getValue();
            if (county.getValue() == "") {
                clab.setUrl(urltemp);
                clab.setValue("");
                return;
            }
            var parent_code = county.getValue();
            clab.setValue("");
            var url = "/azbj/getCodeDetail?codeType=2_1&codeId=" + parent_code;
            clab.setUrl(url);
            if (value != '') {
                clab.setValue(value);
            } else {
                clab.select(0);
            }
        }
    }

    function onValueChanged(e) {
        if ("province" == e) {
            city.setValue("");
            county.setValue("");
            clab.setValue("");
        }
        if ("city" == e) {
            county.setValue("");
            clab.setValue("");
        }
        if ("county" == e) {
            clab.setValue("");
        }
    }

    //电话号码的验证
    function checkPhone(tel) {
        var phone = mini.get("phone").getValue();
        var tel = mini.get("tel").getValue();
        if (tel == '1') {
            if (phone != '' && !(/^1[3456789]\d{9}$/.test(phone))) {
                mini.alert("手机号码有误，请重填");
                return false;
            }
        }
        if (tel == '2') {
            if (tel != '' && !/^((0\d{2,3})-?)(\d{7,8})$/.test(tel)) {
                mini.alert('固定电话有误，请重填');
                return false;
            }
        }
        if (phone == '' && tel == '') {
            mini.alert("请至少输入一个联系方式");
            return false;
        }
    }

    //身份证验证
    function onMouseOut() {
        var id = mini.get("sfzh").getValue();
        if (id != "") {
            if ((id.length == 18 || id.length == 15) && onIDCardsValidation(id)) {
                var strBirthday = "";
                //处理18位的身份证号码从号码中得到生日和性别代码
                if (id.length == 18) {
                    strBirthday = id.substr(6, 4) + "/" + id.substr(10, 2) + "/" + id.substr(12, 2);
                    var num = id[16];
                    if(num%2 ==0){
                        mini.get("xb").setValue("2");
                    } else {
                        mini.get("xb").setValue("1");
                    }
                }
                if (id.length == 15) {
                    strBirthday = "19" + id.substr(6, 2) + "/" + id.substr(8, 2) + "/" + id.substr(10, 2);
                    var num = id[14];
                    if(num%2 ==0){
                        mini.get("xb").setValue("2");
                    } else {
                        mini.get("xb").setValue("1");
                    }
                }
                //时间字符串里，必须是“/”
                var birthDate = new Date(strBirthday);
                var nowDateTime = new Date();
                var age = nowDateTime.getFullYear() - birthDate.getFullYear();
                //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
                if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
                    age--;
                }
                mini.get("nl").setValue(age);
            } else {
                mini.alert("请输入正确的身份证号码");
            }
        }
    }
</script>
</body>
</html>